import axios from 'axios'

export default {
    namespaced: true,
    state:()=>({
        goodsList:[]
    }),
    mutations:{
        updateGoodsList(state,arr){
            state.goodsList = arr
        },
        updateGoodsState(state,payload){
            let index = state.goodsList.findIndex(item => item.id === payload.id)
            state.goodsList[index].goods_state = payload.value
        },
        updateGoodsCount(state,payload){
            let index = state.goodsList.findIndex(item => item.id === payload.id)
            state.goodsList[index].goods_count = payload.value
        },
        undateIsAll(state,payload){
            state.goodsList.forEach(item => item.goods_state = payload)
        }
    },
    actions:{
        async getGoodsList(ctx){
            const arr = await axios.get('https://www.escook.cn/api/cart ')
            ctx.commit('updateGoodsList',arr.data.list)
        }
    },
    getters:{
        allPrice(state){
           return state.goodsList.filter(item => item.goods_state).reduce((sum,item)=> sum+item.goods_price*item.goods_count ,0)
        },
        allCount(state){
           return state.goodsList.filter(item => item.goods_state).reduce((sum,item)=> sum+ item.goods_count ,0)
        }
    }
}